import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.Vector;
import javax.bluetooth.*;
import javax.tools.JavaCompiler;

/**
 * Minimal Device Discovery example.
 */
public class RemoteDeviceDiscovery {

	public static final Vector/* <RemoteDevice> */devicesDiscovered = new Vector();

	public static void main(String[] args) throws IOException,
			InterruptedException {

		final Object inquiryCompletedEvent = new Object();

		devicesDiscovered.clear();

		DiscoveryListener listener = new DiscoveryListener() {

			public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
				System.out.println("Device " + btDevice.getBluetoothAddress()
						+ " found");
				devicesDiscovered.addElement(btDevice);
				try {
					System.out.println("     name "
							+ btDevice.getFriendlyName(false));
				} catch (IOException cantGetDeviceName) {
				}
			}

			public void inquiryCompleted(int discType) {
				System.out.println("Device Inquiry completed!");
				synchronized (inquiryCompletedEvent) {
					inquiryCompletedEvent.notifyAll();
				}
			}

			public void serviceSearchCompleted(int transID, int respCode) {
			}

			public void servicesDiscovered(int transID,
					ServiceRecord[] servRecord) {
			}
		};

		synchronized (inquiryCompletedEvent) {
			boolean started = LocalDevice.getLocalDevice().getDiscoveryAgent()
					.startInquiry(DiscoveryAgent.GIAC, listener);
			if (started) {
				System.out.println("wait for device inquiry to complete...");
				System.out.println(System.nanoTime());
				System.out.println(System.nanoTime());
				System.out.println(System.nanoTime()-System.nanoTime());
				System.out.println(System.currentTimeMillis());
				System.out.println(System.currentTimeMillis());
				System.out.println(System.currentTimeMillis()-System.currentTimeMillis());
				inquiryCompletedEvent.wait();
				System.out.println(devicesDiscovered.size()
						+ " device(s) found");
			}
		}
	}

}
